package java6703.lesson07;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

import java.io.File;
import java.io.IOException;

public class CreateNewFile6703Controller {

    @FXML
    private TextField tfFilename;

    @FXML
    private Label lblMessage;

    @FXML
    void listAll(ActionEvent event) {

    }

    @FXML
    void initialize() {
        tfFilename.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                String filename = tfFilename.getText().trim();
                File file = new File(filename);
                if (!file.exists()) {
                    File parent = file.getParentFile();
                    if (parent != null && !parent.exists()) {
                        if (!parent.mkdirs()) {
                            try {
                                throw new IOException("不能创建目录：" + parent);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        lblMessage.setText("创建目录：" + parent);
                    }
                    try {
                        file.createNewFile();
                        lblMessage.setText("创建新文件：" + file.getAbsolutePath());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    lblMessage.setText("文件已存在");
                }
            }
        });
    }
}

/*
s
*/